
<script>
     $(document).ready(function(){
  $("#hide").click(function(){
    $("#lbl2").hide();
  });
  $("#show").click(function(){
    $("#lbl2").show();
  });     
$("#sel1").change(function(){
         if($("#sel1 :selected").text()=="Захід") { $("#lbl2").show();}
        });
 
  $("#sel1").change(function(){
         if($("#sel1 :selected").text()!="Захід") { $("#lbl2").hide();}
        });
           
  $("#sel2").change(function(){
         if($("#sel2 :selected").text()=="Івано-Франківська") { $("#lbl3").show();}
        });
<div class="large-9 columns">
     
  $("#sel2").change(function(){
         if($("#sel2 :selected").text()!="Івано-Франківська") { $("#lbl3"). hide();}
        });
                
$("#but1").click(function(){
  $.ajax({url:"http://sale-flat/ajax/search", success:function(result){
    alert('dsf');
  }});
}); 
    
    function runajax(){
        
    
    $.ajax({
	   //type: "POST",
	   //data: "email=" + email,
	   url: "/ajax/search",
	  // dataType: "json",
    success: function(result)
	            {  
                 
   	 $("#div1").html(result);
            
    };
    }

  </script>
<div class="row" data-equalizer>
    <div id="div1"> </div>


 <table>
  <thead>
    <tr>
      <th >Адреса</th> 
      <th width="100">Кімнат</th>
      <th width="100">Площа</th>
      <th width="125">Ціна</th>
      <th width="100">Деталі</th>
    </tr>
      
  </thead>
    <tbody>
        <tr>  
<?php

if ($flatMin == "") $flatMin = 0;
if ($flatMax == "") $flatMax = 0;
if ($cMin == "") $cMin = 0;
if ($cMax == "") $cMax = 0;
if ($sel1 == "") $sel1 = 0;
if ($sel2 == "") $sel2 = 0;
if ($sel3 == "") $sel3 = 0;
 
$citys = ORM::factory('city')
 ->where('name', '=', $sel3)
    ->find();  
$regions = ORM::factory('region')
    ->where('name', '=', $sel2)
    ->find();
$locations = ORM::factory('location')
    ->where('name', '=', $sel1)
    ->find();

//if($sel1 == 0 and $flatMin == 0 and $flatMax == 0 and $cMin == 0 and $cMax == 0 )
//{ echo "Виберіть поле для пошуку!";}
//else{
if($sel3 != "0")   ////////////////////////////////////////////////////////////////////////////////////////пошук за містом 
{
    $c1 = ORM::factory('city')
        ->where('name', '=', $sel3)
        ->find_all();
    foreach ($c1 as $city)
    {
        $a1 = ORM::factory('apartment')
            ->where('city_id', '=', $city->id)
            ->find_all();
        if($cMin != "0")////////////////якщо є мінімальна ціна
        {
            if($cMax == "0"){$cMax = "99999999999";}
                
                if($flatMin != "0") ////якщо э мін кімнат 
                {
                    if($flatMax == 0) {$flatMax = "999999";}
                    foreach($a1 as $apartment)
                    {
                        if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                        {
                           echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                        }
                    }
                }
                else
                {
                    if($flatMax != "0")  // якщо є макс кімнат
                    {
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {
                                     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                            }
                        }
                    }
                    else
                    {
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                            {
                                     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                            }
                        }
                    }
                }
            }
            else
            {
                if($cMax != "0")////////////////якщо є макс ціна
                {
                    if($flatMin != "0") ////якщо э мін кімнат
                    {
                        if($flatMax == 0) {$flatMax = "999999";}
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                            }
                        }
                    }
                    else
                    {
                        if($flatMax != "0")  // якщо є макс кімнат
                        {
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                {
                                         echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                                }
                            }
                        }
                        else
                        {
                            foreach($a1 as $apartment)
                        {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                                {
                                         echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                                }
                            }
                        }
                    }
                }
                else
                {
                    if($flatMin != "0")   // якщо э мын ксть квартир
                    {
                        if($flatMax == 0 )
                        {
                            $flatMax = 999999;
                        }
               
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                            {
                                     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                            }
                        }
                    }
                    else
                    {
                        if($flatMax != "0") // якщо є тільки макс кількість квартир
                        {
                     
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                                {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                            } 
                        }
                        else
                        {
                            foreach($a1 as $apartment)
                            {
                                     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                        }
                    } 
                }
            }
    }
}  //////////////////////////////////////////////////////////////////////////////////////////////кінець пошуку по  місті
else
{
    if($sel2 != "0")  /////////////////////////////////////////////////////////////////////////////////////пошук по області 
    {
        $c1 = ORM::factory('city')
            ->where('region_id', '=', $regions->id)
            ->find_all();
        foreach($c1 as $city)
        {
            $a1 = ORM::factory('apartment')
                ->where('city_id', '=', $city->id)
                ->find_all();
            if($cMin != "0")////////////////якщо є мінімальна ціна
            {
                if($cMax == "0"){$cMax = "99999999999";}
                if($flatMin != "0") ////якщо э мін кімнат 
                {
                    if($flatMax == 0) {$flatMax = "999999";}
                    foreach($a1 as $apartment)
                    {
                        if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                        {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                    }
                }
                else
                {
                    if($flatMax != "0")  // якщо є макс кімнат
                    {
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                    else
                    {
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                            {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                }
            }
            else
            {
                if($cMax != "0")////////////////якщо є макс ціна
                {
                    if($flatMin != "0") ////якщо э мін кімнат 
                    {
                        if($flatMax == 0) {$flatMax = "999999";}
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                        }
                    }
                    else
                    {
                        if($flatMax != "0")  // якщо є макс кімнат
                        {
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            }
                        }
                        else
                        {
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                                {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            }
                        }
                    }
                }
                else
                {
                    if($flatMin != "0")   // якщо э мын ксть квартир
                    {
                        if($flatMax == 0 )
                        {
                            $flatMax = 999999;
                        }
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                    else
                    {
                        if($flatMax != "0") // якщо є тільки макс кількість квартир
                        {
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                                {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            } 
                        }
                        else
                        {
                            foreach($a1 as $apartment)
                            {
                                     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    } 
                }
            } //////////кінець пошуку по  ціні 
        }
    }///////////////////////////////////////////////////////////////////////////////////////////////////кінець пошуку по обла
    else
    {
        if($sel1 != "0") /////////////////////////////////////////////////////////////////////////////////пошук по локації 
        {
            $r1 = ORM::factory('region')
                ->where('location_id', '=', $locations->id)
                ->find_all();
            foreach ($r1 as $regions)
            {
                $c1 = ORM::factory('city')
                    ->where('region_id', '=', $regions->id)
                    ->find_all();
                foreach($c1 as $city)
                {
                    $a1 = ORM::factory('apartment')
                        ->where('city_id', '=', $city->id)
                        ->find_all();
                    if($cMin != "0")////////////////якщо є мінімальна ціна
                    {
                        if($cMax == "0"){$cMax = "99999999999";}
                
                        if($flatMin != "0") ////якщо э мін кімнат 
                        {
                            if($flatMax == 0) {$flatMax = "999999";}
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                {
                                      echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                            }
                        }
                        else
                        {
                            if($flatMax != "0")  // якщо є макс кімнат
                            {
                                foreach($a1 as $apartment)
                                {
                                    if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                    {
                                           echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                                    }
                                }
                            }
                            else
                            {
                                foreach($a1 as $apartment)
                                {
                                    if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                                    {
                                           echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                                }
                            }
                        }
                    }
                    else
                    {
                        if($cMax != "0")////////////////якщо є макс ціна
                        {
                            if($flatMin != "0") ////якщо э мін кімнат 
                            {
                                if($flatMax == 0) {$flatMax = "999999";}
                                foreach($a1 as $apartment)
                                {
                                    if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                    {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                                }
                            }
                            else
                            {
                                if($flatMax != "0")  // якщо є макс кімнат
                                {
                                    foreach($a1 as $apartment)
                                    {
                                        if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                        {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                                    }
                                }
                                else
                                {
                                    foreach($a1 as $apartment)
                                    {
                                        if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                                        {
                                                echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if($flatMin != "0")   // якщо э мын ксть квартир
                            {
                                if($flatMax == 0 )
                                {
                                    $flatMax = 999999;
                                }
               
                                foreach($a1 as $apartment)
                                {
                                    if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                                    {
                                            echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                                }
                            }
                            else
                            {
                                if($flatMax != "0") // якщо є тільки макс кількість квартир
                                {
                     
                                    foreach($a1 as $apartment)
                                    {
                                        if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                                        {
                                              echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';}
                                    } 
                                }
                                else
                                {
                    
                                    foreach($a1 as $apartment)
                                    {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                                }
                            } 
                        }
                    } //////////кінець пошуку по  ціні
                }
            }
        }   //////////////////////////////////////////////////////////////////////////кінець пошуку по локації
        else
        {
            if($cMin != "0")  ////////////////////////////////////////////////////////пошук по ціні
            {
                if($cMax == "0"){$cMax = "99999999999";}
                if($flatMin != "0") ////якщо э мін кімнат 
                {
                    if($flatMax == 0) {$flatMax = "999999";}
                    $a1 = ORM::factory('apartment')
                        ->find_all();
                    foreach($a1 as $apartment)
                    {
                        if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                        {
                                echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>';  }
                    }
                }
                else
                {
                    if($flatMax != "0")  // якщо є макс кімнат
                    {$a1 = ORM::factory('apartment')
                        ->find_all();
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                    else
                    {$a1 = ORM::factory('apartment')
                        ->find_all();
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                }
            }
            else
            {
                if($cMax != "0")////////////////якщо є макс ціна
                {
                    if($flatMin != "0") ////якщо э мін кімнат
                    {
                        if($flatMax == 0) {$flatMax = "999999";}
                        $a1 = ORM::factory('apartment')
                        ->find_all();
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                    else
                    {
                        if($flatMax != "0")  // якщо є макс кімнат
                        {$a1 = ORM::factory('apartment')
                        ->find_all();
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax and $apartment->numberRoom >= $flatMin and $apartment <= $flatMax)
                                {     echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            }
                        }
                        else
                        {$a1 = ORM::factory('apartment')
                        ->find_all();
                            foreach($a1 as $apartment)
                        {
                                if ($apartment->price >= $cMin and $apartment->price <= $cMax)
                                {
                                        echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            }
                        }
                    }
                } ////////////////////////////////////////////////////////////////////////////////кінець пошуку по ціні
                else
                {
                    if($flatMin != "0")   ////////////////////////////////////////////////////////пошук по квартирах
                    {
                        if($flatMax == 0 )
                        {
                            $flatMax = 999999;
                        }
               $a1 = ORM::factory('apartment')
                        ->find_all();
                        foreach($a1 as $apartment)
                        {
                            if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    }
                    else
                    {
                        if($flatMax != "0") // якщо є тільки макс кількість квартир
                        {
                     $a1 = ORM::factory('apartment')
                        ->find_all();
                            foreach($a1 as $apartment)
                            {
                                if ($apartment->numberRoom >= $flatMin and $apartment->numberRoom <= $flatMax)
                                {
                                      echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                            } 
                        }
                        else
                        {$a1 = ORM::factory('apartment')
                        ->find_all();
                            foreach($a1 as $apartment)
                            {
                                    echo '<td>'.$apartment->city->region->name.', м.'.$apartment->city->name.', вул.'.$apartment->street .','.$apartment->house.'/'.$apartment->number.'</td>';
                    echo '<td>'.$apartment->numberRoom .'</td>';
                    echo '<td>'.$apartment->area .'</td>';
                    echo '<td>'.$apartment->price .'</td>';
                    echo '<td> <form  method="post">    <button class="button tiny small secondary" type="submit" name="det" >Деталі</button> </td>';
                echo '<input type="hidden" name="idap" value="'.$apartment->id.'">';
                echo '</form></tr>';
                    echo '</tr>'; }
                        }
                    } ////////////////////////////////////////////////////////////////////кін пошуку по квартирах
                }
            }
        }
    }
}

     

?>
          
</tbody>
    </table>
       
